package com.tutu.utils.timer;

import com.tutu.controller.IndexController;
import com.tutu.service.*;
import com.tutu.utils.BeanUtil;
import com.tutu.utils.ConfigUtil;
import com.tutu.utils.WxMsgPushUtil;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class InitDataThread extends Thread {

    CompanyService companyService = (CompanyService) BeanUtil.getBean("companyServiceImpl");

    RoleService roleService = (RoleService) BeanUtil.getBean("roleServiceImpl");

    CodeDictService codeDictService = (CodeDictService) BeanUtil.getBean("codeDictServiceImpl");

    EmissionCodeService emissionCodeService = (EmissionCodeService) BeanUtil.getBean("emissionCodeServiceImpl");

    UserService userService = (UserService) BeanUtil.getBean("userServiceImpl");

    MenuService menuService = (MenuService) BeanUtil.getBean("menuServiceImpl");

    DeviceService deviceService = (DeviceService) BeanUtil.getBean("deviceServiceImpl");

    @Override
    public void run() {
        try {
            // 初始化自动获取一次微信的token
            if (ConfigUtil.getString("refresh.token").equals("true")) WxMsgPushUtil.getToken();

            companyService.loadCacheData();
            roleService.loadCacheData();
            codeDictService.loadCacheData();
            userService.loadCacheData();
            deviceService.loadCacheData(null);
            emissionCodeService.loadCacheData();

            // 自动检测是否需要刷新微信公众号菜单
            if (ConfigUtil.getString("create.menu").equals("true")) {
                menuService.initMenu();
            } else {
                log.info("此次启动无需重新创建微信公众号菜单...");
            }

            // 创建验证码临时缓存空间
            IndexController.getVerifyCode(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
